#import cv2 as cv
import os, shutil
import  MyUtils

    

def reName(path, fileformat={}):
    '''
    fileformat = {'.png', '.bmp'}
    '''
    pathList, wholePaths = MyUtils.recursive_listdir(path)
    for srcFile in pathList:
        split = os.path.splitext(srcFile)
        print(split)
        fileFmt = split[-1]
        imgName = split[0]
        fmtOk = False
        
        for fmt in fileformat:
            if fileFmt != fmt:
                if fileFmt == ".json" and imgName != "mapping_config":
                    print("delete ", srcFile)
                    os.remove(path + "/" + srcFile)
                continue
            else:
                fmtOk = True
                break
        if not fmtOk:
            continue
        if imgName == "mapping_config":
            continue

        srcFile = path + "/" + srcFile
        if(not os.path.exists(srcFile)):
            print(srcFile, " >>>> not exist")
            return

        if 1:
            newName = "sn_"+imgName+"_0"+"sn_"+fileFmt
            os.rename(srcFile, path + "/" + newName)
        else:
            r = srcFile.find("_r")
            c = srcFile.find("_c")
            dot = srcFile.rfind(".")
            row = srcFile[r+2:c]
            col = srcFile[c+2:dot]
            newName = "template_sn_row"+row+"col"+col+"_0sn_"+fileFmt
            os.rename(srcFile, path + "/" + newName)
        print(newName)
    return

if __name__ == "__main__": 
    currentPath = os.getcwd()
    currentPath = "F:\\workespace\\woge-images\\31.5_inch\\model-31.5-\\resource\masks\\mask-pad-V"
    #reName(currentPath, {'.conf','.tmpl'})
    reName(currentPath, {'.png','.json'})